﻿//  Copyright (c) 2011 Ray Liang (http://www.dotnetage.com)
//  Licensed MIT: http://www.opensource.org/licenses/mit-license.php

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DNA.OpenAPI.Atom;

namespace DNA.Mvc.Services
{
    public abstract class AtomEntriesGeneratorBase : Command
    {
        protected abstract IEnumerable<AtomEntry> OnCreateAtomEntries(IDictionary<string, object> data);

        protected virtual IEnumerable<AtomCategory> OnCreateAtomCategories(IDictionary<string, object> data)
        {
            return null;
        }

        public override void Execute(CommandContext context)
        {
            var entries = OnCreateAtomEntries(context.Data);
            var categories = OnCreateAtomCategories(context.Data);

            if ((entries != null) && (entries.Count() > 0))
                ((AtomFeed)context.Bag).Entries.AddRange(entries);

            if ((categories != null) && (categories.Count() > 0))
            {
                if (((AtomFeed)context.Bag).Categories == null) ((AtomFeed)context.Bag).Categories = new List<AtomCategory>();
                ((AtomFeed)context.Bag).Categories.AddRange(categories);
            }
        }

    }
}
